package com.dtm.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;


public class ClientThread extends Thread {
	public static final int READY = 0;
	public static final int UNREADY = 1;
	public static final int CDC = 0;
	public static final int STC = 1;
	public static final int UDL = 2;
	
	private Socket socket;
	private DataOutputStream outputStream;
	private DataInputStream inputStream;
	private SomethingHappenedInterface smtHpnedI;
	private String clientName = "testname";
	private int clientID;
	private int status = READY;

	public ClientThread(Socket socket, int clientID, SomethingHappenedInterface smtHpnedI) {
		this.socket = socket;
		this.clientID = clientID;
		this.smtHpnedI = smtHpnedI;
	}
	
	@Override
	public void run() {
		try {
			inputStream = new DataInputStream(socket.getInputStream());
			outputStream = new DataOutputStream(socket.getOutputStream());
			while (!socket.isClosed()) {
				String message = inputStream.readUTF();
				System.out.println("This is from client ID " + clientID + "\nMessage : " + message);
			}
		} catch (IOException e) {
			System.out.println(clientID);
			smtHpnedI.clientDisconnect(clientID);
			e.printStackTrace();
		}
	}
	
	public void sendToRemoteDevice(String message) {
		try {
			outputStream.writeUTF(message);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getClientName() {
		return clientName;
	}
	
	public int getClientID() {
		return clientID;
	}
	
	public int getClientStatus() {
		return status;
	}
}